import { observable, action } from 'mobx';
import { message } from 'antd';
import http from '@/common/http';
import paramInspect from '@/common/paramInspect';
import bulkVerification from '@/common/bulkVerification'
import { compare } from '@/common/filter.js'

class IntegrationStore {
  constructor(rootStore) {
    this.rootStore = rootStore
  }

  @observable categoryParamFifty = {
    "ascOrDesc": "desc",
    "delState": false,
    "pageIndex": 1,
    "pageSize": 50,
  }
  @observable categoryListFifty = [];
  @observable categoryFiftyLoadSuccess = false;

  @action toGetNewsCategoryFifty = () => {
    http.get('/news/category/page', paramInspect(this.categoryParamFifty))
    .then((response) => {
      if (response.success) {
        this.categoryListFifty = response.data.dataList;
        this.categoryListFifty.forEach((item, index) => {
          item.integral = ""
        })
        this.categoryFiftyLoadSuccess = true
      }
    })
  }

  @action integrationOnChange = (value, index) => {
    this.categoryListFifty[index].integral = value
  }

  // Article
  @observable articleParam = [
    {
      "awards": [
        {
          "amount": "",
          "type": 1
        }
      ],
      "id": 0,
      "maxLimit": "",
      "minLimit": "",
      "name": "article",
      "ruleCode": 104,
      "templateId": 0,
      "templateType": 0
    }, {
      "awards": [
        {
          "amount": "",
          "type": 1
        }
      ],
      "id": 0,
      "maxLimit": "",
      "minLimit": "",
      "name": "article",
      "ruleCode": 104,
      "templateId": 0,
      "templateType": 0
    }, {
      "awards": [
        {
          "amount": "",
          "type": 1
        }
      ],
      "id": 0,
      "maxLimit": null,
      "minLimit": "",
      "name": "article",
      "ruleCode": 104,
      "templateId": 0,
      "templateType": 0
    }
  ]

  @action firstArticleFromOnChange = (e) => {
    this.articleParam[0].minLimit = e.target.value
  }


  @action firstArticleIntegralOnChange = (value) => {
    this.articleParam[0].awards[0].amount = value
  }

  @action secondArticleFromOnChange = (e) => {
    this.articleParam[0].maxLimit = e.target.value
    this.articleParam[1].minLimit = e.target.value
  }

  @action secondArticleToOnChange = (e) => {
    this.articleParam[1].maxLimit = e.target.value
  }

  @action secondArticleIntegralOnChange = (value) => {
    this.articleParam[1].awards[0].amount = value
  }

  @action thirdArticleFromOnChange = (e) => {
    this.articleParam[2].minLimit = e.target.value * 60
  }

  @action thirdArticleIntegralOnChange = (value) => {
    this.articleParam[2].awards[0].amount = value
  }

  @action checkStepOne = () => {
    const verificationArr = []
    this.articleParam.slice(0, 2).forEach((item, index) => {
      if (item.minLimit < item.maxLimit) {
        verificationArr.push(true)
      } else {
        verificationArr.push(false)
      }
    })
    return bulkVerification(verificationArr)
  }

  // Login
  @observable loginParam = [
    {
      "awards": [
        {
          "amount": "",
          "type": 1
        }
      ],
      "id": 0,
      "maxLimit": 0,
      "minLimit": 0,
      "name": "login",
      "ruleCode": 100,
      "templateId": 0,
      "templateType": 0
    }
  ]

  @action loginIntegralOnChange = (value) => {
    this.loginParam[0].awards[0].amount = value
  }

  // Sign in
  @observable signInParam = [
    {
      "awards": [
        {
          "amount": "",
          "type": 1
        }
      ],
      "id": 0,
      "maxLimit": null,
      "minLimit": null,
      "name": "sign",
      "ruleCode": 300,
      "templateId": 0,
      "templateType": 0
    }
  ]

  @action signIntegralOnChange = (value) => {
    this.signInParam[0].awards[0].amount = value
  }

  // Continuous Sign in
  @observable continuousSignList = []

  @action addSignInItem = () => {
    const param = {
      "awards": [
        {
          "amount": "",
          "type": 1
        },
        {
          "amount": "",
          "type": 2
        }
      ],
      "id": 0,
      "maxLimit": 0,
      "minLimit": "",
      "name": "sign",
      "ruleCode": 300,
      "templateId": 0,
      "templateType": 0
    }

    this.continuousSignList.push(param)
  }

  @action deleteSignItem = (index) => {
    this.continuousSignList.splice(index, 1)
  }

  @action signDaysOnChange = (e, index) => {
    this.continuousSignList[index].minLimit = e.target.value
  }

  @action signDaysIntegralOnChange = (value, index) => {
    this.continuousSignList[index].awards[0].amount = value
  }

  @action signDaysPesoOnChange = (e, index) => {
    this.continuousSignList[index].awards[1].amount = e.target.value
  }

  @observable likeShareParam = [
    {
      "awards": [
        {
          "amount": "",
          "type": 1
        }
      ],
      "id": 0,
      "maxLimit": "",
      "minLimit": "",
      "name": "like",
      "ruleCode": 101,
      "templateId": 0,
      "templateType": 0
    }, {
      "awards": [
        {
          "amount": "",
          "type": 1
        }
      ],
      "id": 0,
      "maxLimit": "",
      "minLimit": "",
      "name": "share",
      "ruleCode": 102,
      "templateId": 0,
      "templateType": 0
    }
  ]

  @action likeIntegralOnChange = (value) => {
    this.likeShareParam[0].awards[0].amount = value
  }

  @action shareIntegralOnChange = (value) => {
    this.likeShareParam[1].awards[0].amount = value
  }

  @observable readRewardList = []

  @action addReadRewardItem = () => {
    const param = {
      "awards": [
        {
          "amount": "",
          "type": 1
        }
      ],
      "id": 0,
      "maxLimit": 0,
      "minLimit": "",
      "name": "readReward",
      "ruleCode": 103,
      "templateId": 0,
      "templateType": 0
    }

    this.readRewardList.push(param)
  }

  @action readArticleOnChange = (value, index) => {
    this.readRewardList[index].minLimit = value
  }

  @action readIntegralOnChange = (value, index) => {
    this.readRewardList[index].awards[0].amount = value
  }

  @action deleteReadItem = (index) => {
    this.readRewardList.splice(index, 1)
  }

  @observable contributeParam = [
    {
      "awards": [
        {
          "amount": "",
          "type": 1
        }
      ],
      "id": 0,
      "maxLimit": null,
      "minLimit": null,
      "name": "contribute",
      "ruleCode": 200,
      "templateId": 0,
      "templateType": 0
    }
  ]

  @action contributeIntegralOnChange = (value) => {
    this.contributeParam[0].awards[0].amount = value
  }

  @observable contributeList = []

  @action addcontributeItem = () => {
    const param = {
      "awards": [
        {
          "amount": "",
          "type": 1
        }
      ],
      "id": 0,
      "maxLimit": 0,
      "minLimit": "",
      "name": "contribute",
      "ruleCode": 200,
      "templateId": 0,
      "templateType": 0
    }

    this.contributeList.push(param)
  }

  @action deleteContributeItem = (index) => {
    this.contributeList.splice(index, 1)
  }

  @action articlesOnChange = (value, index) => {
    this.contributeList[index].minLimit = value
  }

  @action articlesIntegrationOnChange = (value, index) => {
    this.contributeList[index].awards[0].amount = value
  }

  @observable paymentParam = [
    {
      "awards": [
        {
          "amount": "",
          "type": 1
        }
      ],
      "id": 0,
      "maxLimit": null,
      "minLimit": null,
      "name": "payment",
      "ruleCode": 201,
      "templateId": 0,
      "templateType": 0
    }
  ]

  @action paymentIntegralOnChange = (value, index) => {
    this.paymentParam[0].awards[0].amount = value
  }

  @observable paymentList = []

  @action addPaymentItem = () => {
    const param = {
      "awards": [
        {
          "amount": "",
          "type": 1
        }
      ],
      "id": 0,
      "maxLimit": 0,
      "minLimit": "",
      "name": "payment",
      "ruleCode": 201,
      "templateId": 0,
      "templateType": 0
    }

    this.paymentList.push(param)
  }

  @action paymentOnChange = (value, index) => {
    this.paymentList[index].minLimit = value
  }

  @action deletePaymentItem = (index) => {
    this.paymentList.splice(index, 1)
  }

  @action paymentListIntegralOnChange = (value, index) => {
    this.paymentList[index].awards[0].amount = value
  }

  @observable sendCategoryParam = []

  @action toConfrim = () => {
    const ruleRequests = this.articleParam.concat(this.loginParam, this.signInParam, this.continuousSignList, this.likeShareParam,
    this.readRewardList, this.contributeParam, this.contributeList, this.paymentParam, this.paymentList)

    this.categoryListFifty.forEach((item, index) => {
      const param = {
        baseIntegral: item.integral,
        categoryId: item.id,
        templateId: 0
      }
      this.sendCategoryParam.push(param)
    })

    const param = {
      "integralActivityCategoryRequests": this.sendCategoryParam,
      "ruleRequests": ruleRequests
    }

    http.post('/activity/template/base/create', param)
    .then((response) => {
      if (response.success) {
        message.success('Success')
      }
    })
  }

  @action updateIntegration = () => {
    const ruleRequests = this.articleParam.concat(this.loginParam, this.signInParam, this.continuousSignList, this.likeShareParam,
    this.readRewardList, this.contributeParam, this.contributeList, this.paymentParam, this.paymentList)

    this.categoryListFifty.forEach((item, index) => {
      console.log(item);
      const param = {
        baseIntegral: item.integral,
        categoryId: item.id,
        templateId: item.templateId
      }
      this.sendCategoryParam.push(param)
    })

    const param = {
      "integralActivityCategoryRequests": this.sendCategoryParam,
      "ruleRequests": ruleRequests
    }

    console.log(param);

    // http.post('/activity/template/base/update', param)
    // .then((response) => {
    //   if (response.success) {
    //     message.success('Success')
    //   }
    // })
  }

  @observable templateLoadSuccess = false;
  @observable isIntegrationCreate = false
  @action toGetTemplate = () => {
    this.rootStore.globalStore.toShowLoding();

    http.get('/activity/template/base/list')
    .then((response) => {
      this.rootStore.globalStore.toHideLoading();
      if (response.success) {
        this.templateLoadSuccess = true;
        if (response.data.integralActivityCategoryDTOS !== null) {
          this.isIntegrationCreate = false
          response.data.integralActivityCategoryDTOS.forEach((item, index) => {
            this.categoryListFifty.forEach((zItem, zIndex) => {
              if (item.categoryId === zItem.id) {
                zItem.integral = item.baseIntegral
              }
            })
          })
          const articlArr = [];
          this.continuousSignList = []
          this.contributeList = []
          this.readRewardList = []
          this.paymentList = []
          response.data.ruleDTOS.forEach((item, index) => {
            if (item.ruleCode === 104) {
              articlArr.push(item)
            }
            if (item.ruleCode === 100) {
              this.loginParam[0].awards[0].amount = item.awards[0].amount
            }
            if (item.ruleCode === 300) {
              if (item.minLimit === null) {
                this.signInParam[0].awards[0].amount = item.awards[0].amount
              } else {
                this.continuousSignList.push(item)
                this.continuousSignList = this.continuousSignList.slice().sort(compare('minLimit'))
              }
            }
            if (item.ruleCode === 101) {
              this.likeShareParam[0].awards[0].amount = item.awards[0].amount
            }
            if (item.ruleCode === 102) {
              this.likeShareParam[1].awards[0].amount = item.awards[0].amount
            }
            if (item.ruleCode === 103) {
              this.readRewardList.push(item)
              this.readRewardList = this.readRewardList.slice().sort(compare('minLimit'))
            }
            if (item.ruleCode === 200) {
              if (item.minLimit === null) {
                this.contributeParam[0].awards[0].amount = item.awards[0].amount
              } else {
                this.contributeList.push(item)
                this.contributeList = this.contributeList.slice().sort(compare('minLimit'))
              }
            }
            if (item.ruleCode === 201) {
              if (item.minLimit === null) {
                this.paymentParam[0].awards[0].amount = item.awards[0].amount
              } else {
                this.paymentList.push(item)
                this.paymentList = this.paymentList.slice().sort(compare('minLimit'))
              }
            }
          })
          // Article Setting
          const articlArrSort = articlArr.sort(compare('minLimit'))
          this.articleParam[0].minLimit = articlArrSort[0].minLimit
          this.articleParam[0].maxLimit = articlArrSort[0].maxLimit
          this.articleParam[0].awards[0].amount = articlArrSort[0].awards[0].amount
          this.articleParam[1].minLimit = articlArrSort[1].minLimit
          this.articleParam[1].maxLimit = articlArrSort[1].maxLimit
          this.articleParam[1].awards[0].amount = articlArrSort[1].awards[0].amount
          this.articleParam[2].minLimit = articlArrSort[2].minLimit
          this.articleParam[2].awards[0].amount = articlArrSort[2].awards[0].amount
        } else {
          this.isIntegrationCreate = true
        }
      }
    })
  }

  // Template
  @observable templateParam = {
    "pageIndex": 1,
    "pageSize": 10,
    "startTime": "",
    "endTime": "",
    "name": ""
  }
  @observable templateList = [];
  @observable templateListTotal = 0;

  @action toGetTemplateList = () => {
    this.rootStore.globalStore.toHideLoading();
    http.get('/integral/activity/list', paramInspect(this.templateParam))
    .then((response) => {
      this.rootStore.globalStore.toHideLoading();
      if (response.success) {
        this.templateList = response.data.dataList;
        this.templateListTotal = response.data.total;
      }
    })
  }

}

export default IntegrationStore;
